package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class GameMainActivity extends Activity implements View.OnClickListener {
    public static final String CALLBACK_LOGIN = "90001";
    public static final String CALLBACK_LOGOUT = "90002";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt_login).setOnClickListener(this);
        findViewById(R.id.bt_creat_role).setOnClickListener(this);
        findViewById(R.id.bt_login_game).setOnClickListener(this);
        findViewById(R.id.bt_up_level).setOnClickListener(this);
        findViewById(R.id.bt_pay).setOnClickListener(this);
        findViewById(R.id.bt_exit).setOnClickListener(this);
        findViewById(R.id.bt_logout).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.bt_login) {
            SDKLogin();
            return;
        }
        if (v.getId() == R.id.bt_creat_role) {
            if (TextUtils.isEmpty(uid)) {
                Toast.makeText(this, "请先登录，谢谢！", Toast.LENGTH_LONG).show();
                return;
            }
            SDKCreateRole(uid, "101", "1008223", "xy123", "101", "1", "", "", "", "", time(), time());
            return;
        }
        if (v.getId() == R.id.bt_login_game) {
            if (TextUtils.isEmpty(uid)) {
                Toast.makeText(this, "请先登录，谢谢！", Toast.LENGTH_LONG).show();
                return;
            }
            SDKLoginGame(uid, "101", "1008223", "xy123", "101", "1", "", "", "", "", time(), time());
            return;
        }
        if (v.getId() == R.id.bt_up_level) {
            if (TextUtils.isEmpty(uid)) {
                Toast.makeText(this, "请先登录，谢谢！", Toast.LENGTH_LONG).show();
                return;
            }
            SDKPlayerInfo(uid, "101", "1008223", "xy123", "101", "1", "", "", "", "", time(), time());
            return;
        }
        if (v.getId() == R.id.bt_pay) {
            if (TextUtils.isEmpty(uid)) {
                Toast.makeText(this, "请先登录，谢谢！", Toast.LENGTH_LONG).show();
                return;
            }
            SDKPay("6", uid, "xy123", "100111", "充值6元", "1008223", "101", orderId(),
                    "\"http://h5sdk.youxigu.com/kaiying/paynotify/game/qmzg\",", "", "",
                    "101", "9", "1");
            return;
        }
        if (v.getId() == R.id.bt_logout) {
            if (TextUtils.isEmpty(uid)) {
                Toast.makeText(this, "请先登录，谢谢！", Toast.LENGTH_LONG).show();
                return;
            }
            SDKLogout();
            return;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void SDKLogin() {
    }

    public void SDKLogout() {
    }

    public void SDKPay(String rmb, String SDKUid, String roleName, String itemId, String productName,
                       String roleId, String serverId, String orderId, String callbackUrl,
                       String extra1, String extra2, String serverName, String roleLevel, String purchaseType) {

    }

    public void showUserAgrement() {
    }

    public void helpService(String SDKUid, String sid, String roleName, String roleId) {

    }

    public void SDKShowCancelAccount() {
    }

    private String uid = "";

    public void sendCallback(String name, String data) {
        if (name.equals(CALLBACK_LOGIN)) {
            try {
                JSONObject json = new JSONObject(data);
                uid = json.getString("userid");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            uid = "";
        }
        Log.e("game xysdk", "sendCallback:" + data);
    }

    public void SDKLoginGame(String gameUserId, String serverId, String roleId, String roleName, String serverName, String rank, String balance, String sociaty, String vipRank, String powerNum, String roleCreateTime, String roleRankUpTime) {

    }

    public void SDKCreateRole(String gameUserId, String serverId, String roleId, String roleName, String serverName, String rank, String balance, String sociaty, String vipRank, String powerNum, String roleCreateTime, String roleRankUpTime) {

    }

    public void SDKPlayerInfo(String gameUserId, String serverId, String roleId, String roleName, String serverName, String rank, String balance, String sociaty, String vipRank, String powerNum, String roleCreateTime, String roleRankUpTime) {
    }

    private String time() {
        return System.currentTimeMillis() + "";
    }

    private String orderId() {
        String number = (1 + Math.random() * 10) + "" + (1 + Math.random() * 10) + "" + (1 + Math.random() * 10) + "";
        return number;
    }

}
